//
//  RegistrationAvatarCellViewModel.swift
//  PorscheRegistration
//
//  Created by Jinzhu Liu on 2023/11/16.
//

import Foundation
import Combine
import UIKit

protocol RegistrationAvatarCellViewModelInputType {
    func showPhotoPicker()
    func updateImage(_ image: UIImage)
    func updateColor(_ color: UIColor)
}

protocol RegistrationAvatarCellViewModelOutputType: RegistrationTitleCellViewModelOutputType {
    var showPhotoPickerPublisher: AnyPublisher<Void, Never> { get }
    
    var image: UIImage { get }
    var color: UIColor { get }
    
    var updateImagePublisher: AnyPublisher<UIImage, Never> { get }
    var updateColorPublisher: AnyPublisher<UIColor, Never> { get }
}

protocol RegistrationAvatarCellViewModelType: RegistrationCellViewModelType {
    var input: RegistrationAvatarCellViewModelInputType { get }
    var output: RegistrationAvatarCellViewModelOutputType { get }
}

class RegistrationAvatarCellViewModel: RegistrationAvatarCellViewModelType {
    var input: RegistrationAvatarCellViewModelInputType { self }
    var output: RegistrationAvatarCellViewModelOutputType { self }
    
    var title: String = "Avatar"
    @Published var image: UIImage = UIImage()
    @Published var color: UIColor = .clear
    
    private var showPhotoPickerSubject = PassthroughSubject<Void, Never>()
    var showPhotoPickerPublisher: AnyPublisher<Void, Never> {
        showPhotoPickerSubject.eraseToAnyPublisher()
    }

    var updateImagePublisher: AnyPublisher<UIImage, Never> {
        $image.eraseToAnyPublisher()
    }
    
    var updateColorPublisher: AnyPublisher<UIColor, Never> {
        $color.eraseToAnyPublisher()
    }

}

extension RegistrationAvatarCellViewModel: RegistrationAvatarCellViewModelInputType {
    func showPhotoPicker() {
        showPhotoPickerSubject.send(())
    }
    
    func updateColor(_ color: UIColor) {
        self.color = color
    }
    
    func updateImage(_ image: UIImage) {
        self.image = image
    }
}

extension RegistrationAvatarCellViewModel: RegistrationAvatarCellViewModelOutputType {

}
